# -*- coding: UTF-8 -*-
import scrapy
import json
import messAround.util.help as util


# steam 热门游戏
# https://store.steampowered.com/stats/content
class SteamStatsSpider(scrapy.Spider):
    name = 'steam_stats'
    allowed_domains = ['store.steampowered.com']
    start_urls = ['http://store.steampowered.com/']

    def start_requests(self):
        urls = [
            'https://steamcdn-a.akamaihd.net/steam/publicstats/contentserver_bandwidth_stacked.jsonp?v=09-11-2020-09'
        ]

        for url in urls:
            yield scrapy.Request(url=url, callback=self.parse, headers=util.default_headers)

    def parse(self, response):
        # print(response.text.replace("jsonpFetch.onCSBandwidth(", '').replace(");", ''))
        result = json.loads(response.text.replace("jsonpFetch.onCSBandwidth(", '').replace(");", ''))
        for index, val in enumerate(result['legend']):
            yield util.make_data({
                'source': 1,
                'no': index,
                'area': val['name'],
                'flow_cur': val['cur'],
                'flow_max': val['max'],
            })

